package fragments;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.Set;

import com.matchcalendar.R;

import dataFormat.CalendarTextData;

import activities.ScheduleActivity;
import algorithm.CalendarAlgorithm;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ScheduleFragment extends Fragment {
	private Calendar startCal = Calendar.getInstance();
	private Calendar specificDay = Calendar.getInstance();
	
	public ScheduleFragment(Calendar startCal, Calendar specificDay) {
		this.startCal.set(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH),
				startCal.get(Calendar.DATE));
		this.specificDay.set(specificDay.get(Calendar.YEAR), specificDay.get(Calendar.MONTH),
				specificDay.get(Calendar.DATE));
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.custom_scheduletable, null);
		Calendar displayCal = Calendar.getInstance();
		
		displayCal.set(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH), startCal.get(Calendar.DATE));
		for (int i=0; i<=6; i++) {
			TextView textViewDate = null;
			TextView textViewDay = null;
			ImageView imageViewDay = null;
			
			switch (i) {
				case 0:
					textViewDate = (TextView)view.findViewById(R.id.textViewDateOfSunday);
					textViewDay = (TextView)view.findViewById(R.id.textViewSunday);
					imageViewDay = (ImageView)view.findViewById(R.id.imageViewSunday);
					break;
				case 1:
					textViewDate = (TextView)view.findViewById(R.id.textViewDateOfMonday);
					textViewDay = (TextView)view.findViewById(R.id.textViewMonday);
					imageViewDay = (ImageView)view.findViewById(R.id.imageViewMonday);
					break;
				case 2:
					textViewDate = (TextView)view.findViewById(R.id.textViewDateOfTuesday);
					textViewDay = (TextView)view.findViewById(R.id.textViewTuesday);
					imageViewDay = (ImageView)view.findViewById(R.id.imageViewTuesday);
					break;
				case 3:
					textViewDate = (TextView)view.findViewById(R.id.textViewDateOfWednesday);
					textViewDay = (TextView)view.findViewById(R.id.textViewWednesday);
					imageViewDay = (ImageView)view.findViewById(R.id.imageViewWednesday);
					break;
				case 4:
					textViewDate = (TextView)view.findViewById(R.id.textViewDateOfThursday);
					textViewDay = (TextView)view.findViewById(R.id.textViewThursday);
					imageViewDay = (ImageView)view.findViewById(R.id.imageViewThursday);
					break;
				case 5:
					textViewDate = (TextView)view.findViewById(R.id.textViewDateOfFriday);
					textViewDay = (TextView)view.findViewById(R.id.textViewFriday);
					imageViewDay = (ImageView)view.findViewById(R.id.imageViewFriday);
					break;
				case 6:
					textViewDate = (TextView)view.findViewById(R.id.textViewDateOfSaturday);
					textViewDay = (TextView)view.findViewById(R.id.textViewSaturday);
					imageViewDay = (ImageView)view.findViewById(R.id.imageViewSaturday);
			}
			setEachDay(displayCal, textViewDate, textViewDay, imageViewDay);
			displayCal.add(Calendar.DATE, 1);
		}
		return view;
	}
	
	private void setEachDay(Calendar displayCal, TextView dateText, TextView dayText, ImageView scheduleView) {
		String theDate = String.valueOf(displayCal.get(Calendar.DATE));
		
		if (CalendarAlgorithm.compareDate(displayCal, specificDay) == 0)
			scheduleView.setImageResource(R.drawable.schedule_deep);
		else
			scheduleView.setImageResource(R.drawable.schedule_shallow);
		dateText.setText(theDate);
		scheduleView.setOnClickListener(new ImageViewDayOnClickListener(displayCal));
	}
	
	private class ImageViewDayOnClickListener implements ImageView.OnClickListener {
		private Calendar theCal = Calendar.getInstance();

		public ImageViewDayOnClickListener(Calendar theCal) {
			this.theCal.set(theCal.get(Calendar.YEAR), theCal.get(Calendar.MONTH), theCal.get(Calendar.DATE));
		}

		@Override
		public void onClick(View v) {
			SparseArray<Set<CalendarTextData>> dayCalDatasMap =
					((ScheduleActivity)getActivity()).getDayCalDatasMap();
			Set<CalendarTextData> calDataSet = dayCalDatasMap.get(theCal.get(Calendar.DAY_OF_WEEK) - 1);
			String[] items;
			
			if (calDataSet.size() == 0) {
				items = new String[1];
				items[0] = "沒有行程";
			}
			else {
				int index = 0;
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
				
				items = new String[calDataSet.size()];
				for (CalendarTextData calData: calDataSet) {
					Calendar cal = Calendar.getInstance();
					
					cal.set(Calendar.HOUR_OF_DAY, calData.getStartHour());
					cal.set(Calendar.MINUTE, calData.getStartMinute());
					items[index] = calData.getTitle() + " " + sdf.format(cal.getTime());
					index++;
				}
			}
			new AlertDialog.Builder(getActivity())
			.setTitle((theCal.get(Calendar.MONTH) + 1) + "月" + theCal.get(Calendar.DATE) + "號")
			.setItems(items, null)
			.setNegativeButton("編輯行程表", null)
			.setPositiveButton("新增揪團", null)
			.show();
		}
	}
}